<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class Role extends Model{

	public function getAllData($status){
		$where ="status = $status";

		//连接role表和role_rule表  role为a表
		$data = Db::name('role')
		->alias('a')
		->join('em_role_rule b','a.id = b.role_id')
		->field('a.*,b.role_id,b.rule_id')
		->where($where)
		->paginate(5);
		// dump($data);
		return $data;
		
	} 

	//添加管理员
	public function addRole($data){
		// dump($data);die;
		$info = Db::name('role')
		->alias('a')
		->join('em_role_rule b','a.id = b.role_id')
		->where("a.role_name",$data['role_name'])
		->find();

		if($info){
			return false;
		}

		unset($data['rule_id']);
		return $this->insertGetId($data);
	}

	public function RoleEdit($data,$rules){
        // dump($data);die;
        $result = Db::name('role')->update($data);
        $changerule = Db::name('role_rule')->where('role_id',$data['id'])->setField('rule_id',$rules);
        return $changerule;
		
    }

    public function findRole($id){
        $data = Db::name('role')
            ->alias('a')
            ->join('role_rule b','b.role_id = a.id')
            ->where('a.id',$id)
            ->field('a.*,b.rule_id')
			->find();
		return $data;
	}

	public function getAllRule(){
        $result = Db::name('Rule')
            ->select();
        return $result;
    }

}